<template>
  <div class="dashboard-container">
    <el-tabs>
      <el-tab-pane label="角色管理" name="0">
        <el-button type="primary" class="btn" @click="showDialog = true">新增角色</el-button>
        <!-- 表格 -->
        <el-table :data="list" border stripe style="width: 100%;margin-top: 15px">
          <el-table-column label="序号" type="index" width="80">
          </el-table-column>
          <el-table-column label="角色" prop="name">
          </el-table-column>
          <el-table-column label="描述" prop="description">
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" size="small">分配权限</el-button>
              <el-button type="success" size="small" @click="onUpdate(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="onDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页布局 -->
        <el-row type="flex" justify="end" style="margin-top: 15px">
          <el-pagination
            layout="prev, pager, next"
            :total="page.total"
            :page-size="page.pagesize"
            @current-change="onCurrentChange"
          ></el-pagination>
        </el-row>
      </el-tab-pane>
      <el-tab-pane label="公司管理">
        <!-- 提示 -->
        <el-alert
          title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
          type="info"
          show-icon
          :closable="false"
          center
        >
        </el-alert>
        <!-- 表单 -->
        <el-form :model="form" label-width="80px" class="demo-ruleForm">
          <el-form-item label="公司名称">
            <el-input v-model="form.name" disabled></el-input>
          </el-form-item>
          <el-form-item label="公司地址">
            <el-input v-model="form.companyAddress" disabled></el-input>
          </el-form-item>
          <el-form-item label="公司邮箱">
            <el-input v-model.number="form.mailbox" disabled></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model.number="form.remarks" disabled></el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>

    <!-- 新增输入框 ==== 弹框  -->
    <el-dialog
      :visible="showDialog"
      v-bind="$attrs"
      :title="formDate.id?'编辑角色':'新建角色'"
      v-on="$listeners"
      @open="onOpen"
      @close="close"
    >
      <el-form ref="formRef" :model="formDate" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formDate.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formDate.description" placeholder="请输入角色描述" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm()">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addRole, deleteRole, getCompanyById, getRoles, getRoleDetail, updateRole } from '@/api/setting.js'

export default {
  name: 'SettingPage',
  components: {},
  props: {},
  data() {
    return {
      showDialog: false, // 定义控制对话框显示变量
      // 表单内容
      form: {},
      list: [],
      page: { // 分页信息 ==> 定义分页相关数据
        page: 1,
        pagesize: 10,
        total: 0
      },
      formDate: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        description: []
      }
    }
  },
  async created() {
    // 获取数据
    this.form = await getCompanyById(this.$store.state.user.userInfo.companyId)
    this.getRoles()
  },
  mounted() {
  },
  methods: {
    onCurrentChange(val) {
      // val点击的页面值 ==> 在事件中保存新的页码值
      this.page.page = val
      // 重新发送请求
      this.getRoles()
    },
    async getRoles() { // 获取数据
      const { rows, total } = await getRoles(this.page)
      this.list = rows
      this.page.total = total
    },
    onAdd() {
      this.showDialog = true // 点击打开对话框
    },
    onOpen() {
    },
    close() {
      this.$refs['formRef'].resetFields()
      // this.$emit('update:visible', false)
      this.formDate = { // 关闭对话框清空数据 防止标题显示错误
        name: undefined,
        description: undefined
      }
      this.showDialog = false
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        if (this.formDate.id) {
          await updateRole(this.formDate)
        } else {
          await addRole(this.formDate)
        }
        // 通知成功
        this.$message.success('操作成功')
        // 刷新列表
        this.getRoles() // 执行新增操作
        this.close()
      })
    },
    async onUpdate(id) {
      // 打开对话框 回显数据
      this.formDate = await getRoleDetail(id)
      this.showDialog = true
    },
    onDel(id) {
      // 确认删除 ==> 定义编辑和删除方法 删除操作
      // 通过代码片段
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        // 发请求
        await deleteRole(id)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.getRoles()
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 20px;

  .demo-ruleForm {
    margin-top: 20px;
  }
}
</style>
